#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    string decodeString(string s) {
        stack<int> nums;
        stack<string> st;
        int i = 0, n = s.size();
        st.push("");
        while (i < n)
        {
            if (s[i] >= '0' && s[i] <= '9')
            {
                int tmp = 0;
                while (s[i] >= '0' && s[i] <= '9')
                {
                    tmp = tmp * 10 + s[i] - '0';
                    i++;
                }
                nums.push(tmp);
            }
            else if (s[i] == '[')
            {
                i++;
                string tmp = "";
                while (s[i] >= 'a' && s[i] <= 'z')
                {
                    tmp += s[i];
                    i++;
                }
                st.push(tmp);
            }
            else if (s[i] == ']')
            {
                string tmp = st.top();
                st.pop();
                int k = nums.top();
                nums.pop();
                while (k--)
                {
                    st.top() += tmp;
                }
                i++;
            }
            else
            {
                string tmp = "";
                while (i < n && s[i] >= 'a' && s[i] <= 'z')
                {
                    tmp += s[i];
                    i++;
                }
                st.top() += tmp;
            }
        }
        return  st.top();
    }
};


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        vector<int> ret;
        if (root == nullptr)
            return ret;
        queue<TreeNode*> q;
        q.push(root);
        while (q.size())
        {
            int sz = q.size();
            int level_max = INT_MIN;
            for (int i = 0; i < sz; i++)
            {
                auto t = q.front();
                q.pop();
                int tmp = t->val;
                level_max = max(level_max, tmp);
                if (t->left)q.push(t->left);
                if (t->right)q.push(t->right);
            }
            ret.push_back(level_max);
        }
        return ret;
    }
};